<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="12">
          <a-form-item label="批次" v-bind="validateInfos.batchId">
	          <j-search-select v-model:value="formData.batchId" dict="tm_project_batch,batch_name,id" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="测试阶段" v-bind="validateInfos.testStageId">
	          <j-search-select v-model:value="formData.testStageId" dict="tm_test_stage,test_stage_name,id" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="关联系统" v-bind="validateInfos.systemId">
	          <j-dict-select-tag v-model:value="formData.systemId" dictCode="tm_project_system,system_name,id" placeholder="请选择关联系统" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="关联功能" v-bind="validateInfos.functionId">
	          <j-tree-select
	            dict="tm_system_module,module_name,id"
	            pidValue=""
	            :disabled="disabled"
	            v-model:value="formData.functionId"
	            @change="(value) => handleFormChange('functionId', value)">
	          </j-tree-select>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="功能名称" v-bind="validateInfos.functionName">
            <a-input v-model:value="formData.functionName" placeholder="请输入功能名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="需求编号" v-bind="validateInfos.reqNo">
            <a-input v-model:value="formData.reqNo" placeholder="请输入需求编号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="测试方法" v-bind="validateInfos.testMethod">
	          <j-dict-select-tag v-model:value="formData.testMethod" dictCode="test_methods" placeholder="请选择测试方法" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="案例性质" v-bind="validateInfos.caseNature">
	          <j-dict-select-tag v-model:value="formData.caseNature" dictCode="case_nature" placeholder="请选择案例性质" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="测试目的" v-bind="validateInfos.caseName">
            <a-input v-model:value="formData.caseName" placeholder="请输入测试目的" :disabled="disabled" ></a-input>
	          <!-- <a-textarea v-model:value="formData.caseName" :rows="4" placeholder="请输入测试目的" :disabled="disabled"/> -->
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="前提条件" v-bind="validateInfos.prerequisite">
	          <a-textarea v-model:value="formData.prerequisite" :rows="4" placeholder="请输入前提条件" :disabled="disabled" style="height:55px"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="数据需求" v-bind="validateInfos.dataRequire">
	          <a-textarea v-model:value="formData.dataRequire" :rows="4" placeholder="请输入数据需求" :disabled="disabled" style="height:55px"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="操作步骤" v-bind="validateInfos.description">
	          <a-textarea v-model:value="formData.description" :rows="4" placeholder="请输入操作步骤" :disabled="disabled" style="height:60px"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="预期结果" v-bind="validateInfos.expectResult">
	          <a-textarea v-model:value="formData.expectResult" :rows="4" placeholder="请输入预期结果" :disabled="disabled" style="height:60px"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="优先级" v-bind="validateInfos.priority">
	          <j-dict-select-tag v-model:value="formData.priority" dictCode="priority" placeholder="请选择优先级" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="编写人" v-bind="validateInfos.caseAuthor">
            <j-search-select v-model:value="formData.caseAuthor" dict="sys_user where status='1',realname,username" placeholder="请选择编写人" :disabled="disabled" />
			      <!-- <j-select-user v-model:value="formData.caseAuthor" :disabled="disabled"/> -->
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="执行人" v-bind="validateInfos.caseExecutor">
            <j-search-select v-model:value="formData.caseExecutor" dict="sys_user where status='1',realname,username" placeholder="请选择执行人" :disabled="disabled" />
			  <!-- <j-select-user v-model:value="formData.caseExecutor" :disabled="disabled"/> -->
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="复核人" v-bind="validateInfos.caseReviewer">
            <j-search-select v-model:value="formData.caseReviewer" dict="sys_user where status='1',realname,username" placeholder="请选择复核人" :disabled="disabled" />
			  <!-- <j-select-user v-model:value="formData.caseReviewer" :disabled="disabled"/> -->
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="开发人员" v-bind="validateInfos.developer">
            <j-search-select v-model:value="formData.developer" dict="sys_user where status='1',realname,username" placeholder="请选择开发人员" :disabled="disabled" />
			  <!-- <j-select-user v-model:value="formData.developer" :disabled="disabled"/> -->
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="开发任务常编码" v-bind="validateInfos.devTaskCode">
            <a-input v-model:value="formData.devTaskCode" placeholder="请输入开发任务常编码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="标签展示" v-bind="validateInfos.tag">
            <a-input v-model:value="formData.tag" placeholder="请输入标签展示" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JSearchSelect from '/@/components/Form/src/jeecg/components/JSearchSelect.vue';
  import JSelectUser from '/@/components/Form/src/jeecg/components/JSelectUser.vue';
  import JTreeSelect from '/@/components/Form/src/jeecg/components/JTreeSelect.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../CaseManage.api';
  import { Form } from 'ant-design-vue';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    batchId: '',   
    testStageId: '',   
    systemId: '',   
    functionId: '',   
    functionName: '',   
    reqNo: '',   
    testMethod: '',   
    caseNature: '',   
    caseName: '',   
    prerequisite: '',   
    dataRequire: '',   
    description: '',   
    expectResult: '',   
    priority: '',   
    caseAuthor: '',   
    caseExecutor: '',   
    caseReviewer: '',   
    developer: '',   
    devTaskCode: '',   
    tag: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({style: { width: '100px' }})
  const wrapperCol = ref<any>({});
  // const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  // const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
    caseName: [{ required: true, message: '请输入测试目的!'},],
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  /**
   * 值改变事件触发
   * @param key
   * @param value
   */
  function handleFormChange(key, value) {
    formData[key] = value;
  }
  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    height: 800px !important;
    overflow-y: auto;
    padding: 14px;
  }
</style>
